{
  "cells": [
    {
      "metadata": {
        "id": "-n3Rngfddxii"
      },
      "cell_type": "markdown",
      "source": [
        "This notebook is a basic tutorial that demonstrates how to configure a simulation using Concordia."
      ]
    },
    {
      "metadata": {
        "id": "T6qSDTZtdeXk"
      },
      "cell_type": "markdown",
      "source": [
        "<a href=\"https://colab.research.google.com/github/google-deepmind/concordia/blob/main/examples/alice.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "o4NhU-Z6cbop"
      },
      "cell_type": "code",
      "source": [
        "# @title Colab-specific setup (use a CodeSpace to avoid the need for this).\n",
        "try:\n",
        "  %env COLAB_RELEASE_TAG\n",
        "except:\n",
        "  pass  # Not running in colab.\n",
        "else:\n",
        "  %pip install --ignore-requires-python --requirement 'https://raw.githubusercontent.com/google-deepmind/concordia/main/examples/requirements.in' 'git+https://github.com/google-deepmind/concordia.git#egg=gdm-concordia'\n",
        "  %pip list"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "R8N4hGX5ci_d"
      },
      "cell_type": "code",
      "source": [
        "# @title Imports\n",
        "\n",
        "from concordia.contrib import language_models as language_model_utils\n",
        "import concordia.prefabs.entity as entity_prefabs\n",
        "import concordia.prefabs.game_master as game_master_prefabs\n",
        "from concordia.prefabs.simulation import generic as simulation\n",
        "from concordia.typing import prefab as prefab_lib\n",
        "from concordia.utils import helper_functions\n",
        "from IPython import display\n",
        "import numpy as np\n",
        "import sentence_transformers"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "EkEWOSSYcJ2b"
      },
      "cell_type": "code",
      "source": [
        "# @title Language Model Selection: provide key or select DISABLE_LANGUAGE_MODEL\n",
        "\n",
        "# By default this colab uses models via an external API so you must provide an\n",
        "# API key. TogetherAI offers open weights models from all sources.\n",
        "\n",
        "API_KEY = ''  # @param {type: 'string'}\n",
        "# See concordia/language_model/utils.py\n",
        "API_TYPE = 'openai'  # e.g. 'together_ai' or 'openai'.\n",
        "MODEL_NAME = (  # for API_TYPE = 'together_ai', we recommend MODEL_NAME = 'google/gemma-3-27b-it'\n",
        "    'gpt-5'\n",
        ")\n",
        "# To debug without spending money on API calls, set DISABLE_LANGUAGE_MODEL=True\n",
        "DISABLE_LANGUAGE_MODEL = False"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "Za2zOpeTci8_"
      },
      "cell_type": "code",
      "source": [
        "# @title Use the selected language model\n",
        "\n",
        "# Note that it is also possible to use local models or other API models,\n",
        "# simply replace this cell with the correct initialization for the model\n",
        "# you want to use.\n",
        "\n",
        "if not DISABLE_LANGUAGE_MODEL and not API_KEY:\n",
        "  raise ValueError('API_KEY is required.')\n",
        "\n",
        "model = language_model_utils.language_model_setup(\n",
        "    api_type=API_TYPE,\n",
        "    model_name=MODEL_NAME,\n",
        "    api_key=API_KEY,\n",
        "    disable_language_model=DISABLE_LANGUAGE_MODEL,\n",
        ")"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "KC0dbTwLci6k"
      },
      "cell_type": "code",
      "source": [
        "# @title Setup sentence encoder\n",
        "\n",
        "if DISABLE_LANGUAGE_MODEL:\n",
        "  embedder = lambda _: np.ones(3)\n",
        "else:\n",
        "  st_model = sentence_transformers.SentenceTransformer(\n",
        "      'sentence-transformers/all-mpnet-base-v2'\n",
        "  )\n",
        "  embedder = lambda x: st_model.encode(x, show_progress_bar=False)"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "yN8NEgajci3_"
      },
      "cell_type": "code",
      "source": [
        "test = model.sample_text(\n",
        "    'Is societal and technological progress like getting a clearer picture of '\n",
        "    'something true and deep?'\n",
        ")\n",
        "print(test)"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "Ya7BvCG1cwNY"
      },
      "cell_type": "code",
      "source": [
        "# @title Load prefabs from packages to make the specific palette to use here.\n",
        "\n",
        "prefabs = {\n",
        "    **helper_functions.get_package_classes(entity_prefabs),\n",
        "    **helper_functions.get_package_classes(game_master_prefabs),\n",
        "}"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "hYkErVfPRKDC"
      },
      "cell_type": "code",
      "source": [
        "# @title Print menu of prefabs\n",
        "\n",
        "display.display(\n",
        "    display.Markdown(helper_functions.print_pretty_prefabs(prefabs))\n",
        ")"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "bREMbhkVcwQH"
      },
      "cell_type": "code",
      "source": [
        "# @title Configure instances.\n",
        "\n",
        "instances = [\n",
        "    prefab_lib.InstanceConfig(\n",
        "        prefab='basic__Entity',\n",
        "        role=prefab_lib.Role.ENTITY,\n",
        "        params={\n",
        "            'name': 'Alice',\n",
        "        },\n",
        "    ),\n",
        "    prefab_lib.InstanceConfig(\n",
        "        prefab='basic__Entity',\n",
        "        role=prefab_lib.Role.ENTITY,\n",
        "        params={\n",
        "            'name': 'White Rabbit',\n",
        "            'goal': 'Get to the queen on time',\n",
        "        },\n",
        "    ),\n",
        "    prefab_lib.InstanceConfig(\n",
        "        prefab='generic__GameMaster',\n",
        "        role=prefab_lib.Role.GAME_MASTER,\n",
        "        params={\n",
        "            'name': 'default rules',\n",
        "            'acting_order': 'fixed',\n",
        "        },\n",
        "    ),\n",
        "]"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "LvZQZ2qmcwSU"
      },
      "cell_type": "code",
      "source": [
        "config = prefab_lib.Config(\n",
        "    default_premise=(\n",
        "        'Alice is sitting on the bank, being quite bored and tired by reading a'\n",
        "        ' book with no pictures or conversations, when suddenly a white rabbit'\n",
        "        ' with pink eyes ran close by her. '\n",
        "    ),\n",
        "    default_max_steps=5,\n",
        "    prefabs=prefabs,\n",
        "    instances=instances,\n",
        ")"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "dJiP0nFzdHwM"
      },
      "cell_type": "markdown",
      "source": [
        "# The simulation"
      ]
    },
    {
      "metadata": {
        "id": "6-aNTmgacwbk"
      },
      "cell_type": "code",
      "source": [
        "# @title Initialize the simulation\n",
        "runnable_simulation = simulation.Simulation(\n",
        "    config=config,\n",
        "    model=model,\n",
        "    embedder=embedder,\n",
        ")"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "dl1wJP9EcweG"
      },
      "cell_type": "code",
      "source": [
        "# @title Run the simulation\n",
        "raw_log = []\n",
        "results_log = runnable_simulation.play(max_steps=5, raw_log=raw_log)"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "oQvo3alwdMgE"
      },
      "cell_type": "code",
      "source": [
        "# @title Display the log\n",
        "display.HTML(results_log)"
      ],
      "outputs": [],
      "execution_count": null
    },
    {
      "metadata": {
        "id": "S8TdyvoYdSV8"
      },
      "cell_type": "markdown",
      "source": [
        "```\n",
        "Copyright 2024 DeepMind Technologies Limited.\n",
        "\n",
        "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "you may not use this file except in compliance with the License.\n",
        "You may obtain a copy of the License at\n",
        "\n",
        "    https://www.apache.org/licenses/LICENSE-2.0\n",
        "\n",
        "Unless required by applicable law or agreed to in writing, software\n",
        "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "See the License for the specific language governing permissions and\n",
        "limitations under the License.\n",
        "```"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "private_outputs": true,
      "provenance": [],
      "toc_visible": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
